package com.dhecp.project.system.service.impl;

import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysVersion;
import com.dhecp.project.system.mapper.SysVersionItemMapper;
import com.dhecp.project.system.mapper.SysVersionMapper;
import com.dhecp.project.system.service.SysVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysVersionServiceImpl
 * @Description TODO
 * @Author alula
 * @Date 2020-03-12 10:59
 */
@Service
public class SysVersionServiceImpl implements SysVersionService {

    @Autowired
    private SysVersionMapper sysVersionMapper;

    @Autowired
    private SysVersionItemMapper sysVersionItemMapper;


    @Override
    public List<SysVersion> queryVersionList(Map<String, Object> params) {
        return  sysVersionMapper.findVersionList(params);
    }

    @Override
    public SysVersion queryVersionById(String guid) {
        return  sysVersionMapper.findVersionById(guid);
    }

    @Override
    public AjaxResult insertVersion(SysVersion sysVersion) {
        if (sysVersion == null) {
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", sysVersion));
        }
        if (queryVersionIsExistVno(sysVersion.getVersion(), sysVersion.getGuid()) == true) {
            return AjaxResult.error(MessageUtils.message("module.error.paramexistfail", sysVersion.getVersion()));
        }
        sysVersion.setCreateUser(ShiroUtils.getMandatorId());
        sysVersionMapper.insert(sysVersion);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    @Override
    public AjaxResult updateVersion(SysVersion sysVersion) {
        if (sysVersion == null) {
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", sysVersion));
        }
        if (queryVersionIsExistVno(sysVersion.getVersion(), sysVersion.getGuid()) == true) {
            return AjaxResult.error(MessageUtils.message("module.error.paramexistfail", sysVersion.getVersion()));
        }
        sysVersion.setUpdateUser(ShiroUtils.getMandatorId());
        sysVersionMapper.update(sysVersion);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    @Override
    public AjaxResult deleteVersion(String guid) {
        if (StringUtils.isEmpty(guid)) {
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", guid));
        }
        sysVersionMapper.deleteVersionByGuid(guid);
        sysVersionItemMapper.deleteVersionItemByGuid(guid);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    @Override
    public Boolean queryVersionIsExistVno(String version, String guid) {
        if (StringUtils.isEmpty(version)) {
            return true;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("version",version);
        params.put("guid",guid);
        int count = sysVersionMapper.findVersionByVersionCount(params);
        return  (count > 0) ? true :false;
    }
}
